package com.fisicaapp;


import java.io.File;
import java.io.IOException;
import java.util.Locale;

import jxl.CellView;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.UnderlineStyle;
import jxl.write.Formula;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class WriteExcel {

	private WritableCellFormat timesBoldUnderline;
	private WritableCellFormat times;
	private String inputFile;
	private String [] captions;
	private double [] t, x, y;

	
	public WriteExcel(double []t, double []x, double []y, String [] captions){
		this.t = new double[t.length];
		this.x = new double[x.length];
		this.y = new double[y.length];
		this.captions = new String[captions.length];
		
		System.arraycopy(t, 0, this.t, 0, t.length);
		System.arraycopy(x, 0, this.x, 0, x.length);
		System.arraycopy(y, 0, this.y, 0, y.length);
		System.arraycopy(captions, 0, this.captions, 0, captions.length);
	}
	
	
	public void setOutputFile(String inputFile) {
		this.inputFile = inputFile;
	}

	public void write() throws IOException, WriteException {
		File file = new File(inputFile);
		WorkbookSettings wbSettings = new WorkbookSettings();
		wbSettings.setLocale(new Locale("es", "ES"));
		WritableWorkbook workbook = Workbook.createWorkbook(file,
				wbSettings);
		workbook.createSheet("Reporte_Graficos", 0);
		WritableSheet excelSheet = workbook.getSheet(0);
		createLabel(excelSheet);
		createContent(excelSheet);

		workbook.write();
		workbook.close();
	}

	private void createLabel(WritableSheet sheet) throws WriteException {
		// Lets create a times font
		WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
		// Define the cell format
		times = new WritableCellFormat(times10pt);
		// Lets automatically wrap the cells
		times.setWrap(true);

		// Create create a bold font with unterlines
		WritableFont times10ptBoldUnderline = new WritableFont(
				WritableFont.TIMES, 10, WritableFont.BOLD, false,
				UnderlineStyle.SINGLE);
		timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);
		// Lets automatically wrap the cells
		timesBoldUnderline.setWrap(true);

		CellView cv = new CellView();
		cv.setFormat(times);
		cv.setFormat(timesBoldUnderline);
		cv.setAutosize(true);

		// Write a few headers
		addCaption(sheet, 0, 0, captions[0]);
		addCaption(sheet, 1, 0, captions[1]);
		addCaption(sheet, 2, 0, captions[2]);

	}

	private void createContent(WritableSheet sheet) throws WriteException,
			RowsExceededException {
		// Write a few number
		
		
		for (int i = 1; i < t.length; i++) {
			// First column
			addNumber(sheet, 0, i, t[i]);
			// Second column
			addNumber(sheet, 1, i, x[i]);
			// Third column
			addNumber(sheet, 2, i, y[i]);
		}
		// Lets calculate the sum of it
		
		addCaption(sheet, 4, 0, "Prom. "+captions[1]);
		
		
		StringBuffer buf = new StringBuffer();
		buf.append("PROMEDIO(B2:B"+String.valueOf(x.length)+")");
		Formula f = new Formula(4, 1, buf.toString());
		sheet.addCell(f);
		
		addCaption(sheet, 5, 0, "Prom. "+captions[2]);
		buf = new StringBuffer();
		buf.append("PROMEDIO(C2:C"+String.valueOf(y.length)+")");
		f = new Formula(5, 1, buf.toString());		
		sheet.addCell(f);

		/*
		// Now a bit of text
		for (int i = 12; i < 20; i++) {
			// First column
			addLabel(sheet, 0, i, "Boring text " + i);
			// Second column
			addLabel(sheet, 1, i, "Another text");
		}*/
	}

	private void addCaption(WritableSheet sheet, int column, int row,
			String s) throws RowsExceededException, WriteException {
		Label label;
		label = new Label(column, row, s, timesBoldUnderline);
		sheet.addCell(label);
	}

	private void addNumber(WritableSheet sheet, int column, int row,
			double integer) throws WriteException, RowsExceededException {
		Number number;
		number = new Number(column, row, integer, times);
		
		sheet.addCell(number);
		
	}

	private void addLabel(WritableSheet sheet, int column, int row, String s)
			throws WriteException, RowsExceededException {
		Label label;
		label = new Label(column, row, s, times);
		sheet.addCell(label);
	}

} //FIN CLASE EXPORTAR

